package com.imti.mall.order.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.imti.mall.model.order.pojo.*;
import com.imti.mall.model.order.query.OmsOrderDelivery;
import com.imti.mall.model.order.query.OmsOrderQuery;
import com.imti.mall.model.order.vo.OmsOrderAndItem;
import com.imti.mall.model.order.vo.OmsOrderDetail;
import com.imti.mall.model.order.vo.OmsOrderMoneyInfo;
import com.imti.mall.model.order.vo.OmsOrderReceiverInfo;
import com.imti.mall.order.mapper.OrderItemMapper;
import com.imti.mall.order.mapper.OrderMapper;
import com.imti.mall.order.mapper.OrderOperateHistoryMapper;
import com.imti.mall.order.service.OmsOrderReturnApplyService;
import com.imti.mall.order.service.OmsOrderReturnReasonService;
import com.imti.mall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OmsOrderReturnApplyService omsOrderReturnApplyService;
    @Autowired
    private OmsOrderReturnReasonService omsOrderReturnReasonService;
    @Autowired
    private OrderOperateHistoryMapper orderOperateHistoryMapper;


    @Override
    @Transactional
    public List<OmsOrder> list(OmsOrderQuery omsOrderQuery, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return orderMapper.getList(omsOrderQuery);
    }

    @Override
    @Transactional
    public OmsOrderDetail detail(Long orderId) {
        OmsOrder omsOrder = this.getById(orderId);
        List<OmsOrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OmsOrderItem>().eq(OmsOrderItem::getOrderId, orderId));
        List<OmsOrderOperateHistory> orderOperateHistoryList = orderOperateHistoryMapper.selectList(new LambdaQueryWrapper<OmsOrderOperateHistory>().eq(OmsOrderOperateHistory::getOrderId, orderId));
        OmsOrderDetail orderDetail = new OmsOrderDetail();
        BeanUtil.copyProperties(omsOrder, orderDetail);
        orderDetail.setOrderItemList(orderItemList);
        orderDetail.setHistoryList(orderOperateHistoryList);
        return orderDetail;
    }
  /**
   * 根据订单id删除订单
   */
  @Override
  public int deletebyId(Long id) {
    return orderMapper.deletebyId(id);
  }

  /**
   * 根据订单id关闭订单
   */
  @Override
  public int closeOrderById(OmsOrder omsOrder) {
    return orderMapper.closeOrderById(omsOrder);
  }

  /**
   * 根据订单id对订单进行备注
   *
   * @param omsOrder
   * @return
   */
  @Override
  public int noteById(OmsOrder omsOrder) {
    return orderMapper.noteById(omsOrder);
  }

  /**
   * 发货,插入快递公司以及单号,修改订单状态
   *
   * @param omsOrderDelivery
   * @return
   */
  @Override
  public int updateOrderForDelivery(OmsOrderDelivery omsOrderDelivery) {
    return orderMapper.updateOrderForDelivery(omsOrderDelivery);
  }

  @Override
  public List<OmsOrderAndItem> findOrderAndItemById(OmsOrderQuery omsOrderQuery, Integer pageSize, Integer pageNum) {
    PageHelper.startPage(pageNum, pageSize);
    return orderMapper.getOrderById(omsOrderQuery);
  }

  @Override
  public int updateReceiverInfo(OmsOrderReceiverInfo omsOrderReceiverInfo) {
    return orderMapper.updateReceiverInfo(omsOrderReceiverInfo);
  }

  @Override
  public int updateOrderMoney(OmsOrderMoneyInfo omsOrderMoneyInfo) {
    return orderMapper.updateOrderMoney(omsOrderMoneyInfo);
  }
  @Override
  @Transactional
  public boolean addMethod(List<OmsOrderReturnApply> omsOrderReturnApplyList, OmsOrderReturnReason omsOrderReturnReason) throws ParseException {

    //添加退货申请记录
    boolean applyResult = true;
    if (ObjectUtil.isNull(omsOrderReturnApplyList)) {
      return false;
    } else {
      /*除了前端传入的还需
           1、createTime 申请时间
           2、status 申请状态
      **/
      for (OmsOrderReturnApply omsOrderReturnApply : omsOrderReturnApplyList) {
        omsOrderReturnApply.setStatus(0);
      }
      applyResult = omsOrderReturnApplyService.saveBatch(omsOrderReturnApplyList);
    }

    //添加退货原因记录
    boolean reasonResult = true;
    if(ObjectUtil.isNull(omsOrderReturnReason)){
      return false;
    }else{
      reasonResult = omsOrderReturnReasonService.save(omsOrderReturnReason);
    }

        //添加订单操作历史记录
        boolean historyResult = true;
        Long orderId = omsOrderReturnApplyList.get(0).getOrderId();
        String operateMan = "用户";
        Date createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(new Date().getTime()));
        //需要调用omsorder表的status 0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        Integer orderStatus = null;
        String note = "";
        OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory(orderId, operateMan, createTime, orderStatus, note);
        int tempResult = orderOperateHistoryMapper.updateById(omsOrderOperateHistory);
        if (tempResult < 0) {
            historyResult = false;
        }
        if (reasonResult == true && applyResult == true && historyResult == true) {
            return true;
        } else {
            return false;
        }
    }



    @Override
    public int updateOrderPay(String orderSn, Integer payType) {
        return orderMapper.updateOrderPay(orderSn,payType);
    }

    @Override
    public int insertAll(OmsOrder omsOrder) {
        return orderMapper.insertAll(omsOrder);
    }

}
